package org.kazuhiko.rpg.core;

import java.awt.Rectangle;

import org.lwjgl.util.vector.Vector3f;

public class Physics
{	
	public enum GravityAcceleration
	{
		EARTH( 9.8 ),
		MOON( 1.6 );
		
		GravityAcceleration( double speed )
		{
			this.speed = speed;
		}
		
		double speed;
		
		public double getSpeed()
		{
			return speed;
		}
	}
	
	public static double getWeight( double mass, GravityAcceleration acceleration )
	{
		double w = mass * acceleration.speed;
		
		return w;
	}
		
	public static double distance( Vector3f posOne, Vector3f posTwo )
	{
		return Physics.distance( posOne.x, posOne.y, posOne.z, posTwo.x, posTwo.y, posTwo.z );
	}
	
	public static double distance( float ax, float ay, float az, float bx, float by, float bz )
	{
		double D = 0;
		
		double dx = 0;
		double dy = 0;
		double dz = 0;
		
		dx = ax - bx;
		dy = ay - by;
		dz = az - bz;
		
		D = Math.sqrt( ( Math.pow( dx, 2 ) ) + ( Math.pow( dy, 2 ) ) + ( Math.pow( dz, 2 ) ) );
		
		return D;
	}
	
	public static boolean isColliding( Drawable first, Drawable second )
	{
		if( first.getPosition().y - ( first.getHeight() / 2 ) < second.getPosition().y + ( second.getHeight() / 1 ) &&
			//first.getPosition().y > second.getPosition().y - second.getHeight() &&
			first.getPosition().x + first.getWidth() > second.getPosition().x &&
			first.getPosition().x < second.getPosition().x + second.getWidth() )
		{
			Rectangle firstRec = new Rectangle( (int)first.getPosition().x, (int)first.getPosition().z, (int)first.getWidth(), (int)first.getDepth() );
			Rectangle secondRec = new Rectangle( (int)second.getPosition().x, (int)second.getPosition().z, (int)second.getWidth(), (int)second.getDepth() );
			
			if( firstRec.intersects( secondRec ) )
			{				
				return true;
			}
		}
		
		return false;
	}
	
	/*
	public static void fall( Drawable obj, GravityAcceleration acceleration )
	{

		
		//double speed = ( acceleration.speed * obj.getFallingtime() ) + obj.getStartSpeed();
		//double distance = -0.5 * acceleration.speed * Math.pow( obj.getFallingtime(), 2 ) + -obj.getStartSpeed() * obj.getFallingtime() + obj.getStartHeight();
		
		if( obj.getPosition().y > 0 )
		{
			obj.setPosition( new Vector3f( obj.getPosition().x, obj.getPosition().y + ( (float)distance / 1000 ), obj.getPosition().z ) );
		}		
	}
	*/
	
	//F = mg
	//F = weight in Newetons ( N )
	//m = mass in kg
	//g = accelration as m/s^2
	
	//Fgrav = G * M * m / R ^ 2
	/*
	public static double Fgravity( Drawable obj )
	{
		//Fgrav = G * M * m / R ^ 2
		double m = obj.getMass();
		double r = distance( EARTH_POS, obj.getPosition() );
		double Fgravity = ( G * m * EARTH_MASS ) / ( Math.pow( r, 2 ) );
		
		return Fgravity;
	}
	*/
}
